//
//  RentManager.m
//  RentTable
//
//  Created by xchuan xchuan on 2024/12/2.
//

#import "RentManager.h"

@interface RentManager()

@property (nonatomic, strong) NSMutableArray<RentModel *> *rentModels;

@end

@implementation RentManager

+ (instancetype)sharedManager {
    static RentManager *sharedManager = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        sharedManager = [[RentManager alloc] init];
    });
    return sharedManager;
}

- (void)loadRentDataFromFile {
    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"rentData" ofType:@"json"];
    NSData *data = [NSData dataWithContentsOfFile:filePath];
    
    NSError *error = nil;
    NSArray *jsonArray = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:&error];
    
    if (error) {
        NSLog(@"Error reading JSON: %@", error.localizedDescription);
        return;
    }
    
    NSMutableArray *rentModels = [NSMutableArray array];
    
    for (NSDictionary *rentDict in jsonArray) {
        RentModel *model = [[RentModel alloc] initWithDictionary:rentDict];
        [rentModels addObject:model];
    }
    
    self.rentModels = rentModels;
}

- (RentModel *)getRentModelWithRentId:(NSInteger)rentId {
    for (RentModel *model in self.rentModels) {
        if (model.rentId == rentId) {
            return model;
        }
    }
    return nil;
}

- (void)updateRentModel:(RentModel *)model {
    for (NSInteger i = 0; i < self.rentModels.count; i++) {
        RentModel *existingModel = self.rentModels[i];
        if (existingModel.rentId == model.rentId) {
            self.rentModels[i] = model;
            break;
        }
    }
    
    [self saveRentDataToFile];
}

- (void)saveRentDataToFile {
    // 获取沙盒中的Documents目录路径
    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"rentData" ofType:@"json"];

    NSMutableArray *jsonArray = [NSMutableArray array];
    
    for (RentModel *model in self.rentModels) {
        [jsonArray addObject:[model toDictionary]];
    }
    
    NSData *data = [NSJSONSerialization dataWithJSONObject:jsonArray options:NSJSONWritingPrettyPrinted error:nil];
    
    // 将修改后的数据写入文件
    NSError *error = nil;
    [data writeToFile:filePath options:NSDataWritingAtomic error:&error];
    if (error) {
        NSLog(@"Error writing to file: %@", error.localizedDescription);
    }
    
    [self loadRentDataFromFile];
}


- (NSMutableArray<RentModel *> *)getAllRentData {
    return self.rentModels;
}

//获取 rentCollection == true 的所有 RentModel 实例
- (NSArray<RentModel *> *)getAllRentModelsWithCollectionTrue {
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"rentCollection == YES"];
    NSArray *filteredModels = [self.rentModels filteredArrayUsingPredicate:predicate];
    
    return filteredModels;
}

//获取 rentCart == true 的所有 RentModel 实例
- (NSArray<RentModel *> *)getAllRentModelsWithCartTrue {
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"rentCart == YES"];
    NSArray *filteredModels = [self.rentModels filteredArrayUsingPredicate:predicate];
    
    return filteredModels;
}

// 获取所有九球租赁模型
- (NSMutableArray<RentModel *> *)getAllRentModelsWithRentTypeNineBall {
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"rentType == %@", @"九球"];
    NSArray *filteredModels = [self.rentModels filteredArrayUsingPredicate:predicate];
    return [NSMutableArray arrayWithArray:filteredModels];  // 返回 NSMutableArray
}

// 获取所有斯诺克租赁模型
- (NSMutableArray<RentModel *> *)getAllRentModelsWithRentTypeSnooker {
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"rentType == %@", @"斯诺克"];
    NSArray *filteredModels = [self.rentModels filteredArrayUsingPredicate:predicate];
    return [NSMutableArray arrayWithArray:filteredModels];  // 返回 NSMutableArray
}

// 获取所有中八台球租赁模型
- (NSMutableArray<RentModel *> *)getAllRentModelsWithRentTypeEightBall {
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"rentType == %@", @"中八台球"];
    NSArray *filteredModels = [self.rentModels filteredArrayUsingPredicate:predicate];
    return [NSMutableArray arrayWithArray:filteredModels];  // 返回 NSMutableArray
}

@end
